package com.wswy.wzcx.ui.user

import android.os.Bundle
import android.text.Editable
import android.text.TextUtils
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.view.View
import com.blankj.utilcode.util.KeyboardUtils
import com.che.libcommon.utils.ToastUtils
import com.wswy.wzcx.R
import com.wswy.wzcx.api.Api
import com.wswy.wzcx.module.base.ApiCallback
import com.wswy.wzcx.module.base.CBaseFragment
import com.wswy.wzcx.module.base.onResult
import com.wswy.wzcx.statistics.StatisticsId
import com.wswy.wzcx.ui.other.TextWatcherIMPL
import kotlinx.android.synthetic.main.fragment_password_verify.*

/**
 * 密码 验证
 */
class PasswordVerifyFragment : BaseVerifyFragment(){


    override fun getContentLayoutId(): Int {
        return R.layout.fragment_password_verify
    }


    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        et_pwd.addTextChangedListener(object : TextWatcherIMPL(){
            override fun afterTextChanged(s: Editable?) {
                super.afterTextChanged(s)
                inputCheck()
            }
        })
        img_et_line2_action.setOnClickListener {

            it.isActivated = !it.isActivated

            if (it.isActivated){
                et_pwd.transformationMethod = HideReturnsTransformationMethod.getInstance()
            }else{
                et_pwd.transformationMethod = PasswordTransformationMethod.getInstance()
            }

            et_pwd.setSelection(et_pwd.text?.length?:0)

        }

        btn_submit.setOnClickListener {

            val pwd =et_pwd.text?.toString()


            if (pwd.isNullOrEmpty()){
                ToastUtils.showText("请输入密码")
                return@setOnClickListener
            }

            when(loginHolderViewModel.stepState.type){

                LoginHolderViewModel.TYPE_REG -> {
                    sendClickEvent(StatisticsId.u_zc_tj_mima)
                    doRegSubmit(pwd)
                }

                LoginHolderViewModel.TYPE_FORGET_PWD -> {
                    sendClickEvent(StatisticsId.u_dl_wjmm_tj_mima)
                    doResetSubmit(pwd)
                }

            }

            //doSubmit(pwd!!)

        }


        KeyboardUtils.showSoftInput(et_pwd)

    }


    private fun doResetSubmit(pwd:String){
        val phone =  loginHolderViewModel.stepState.phone
        val ticket = loginHolderViewModel.stepState.ticket

        lockLoad()

        btn_submit.loading("设置中...")

        loginHolderViewModel.addRun(Api.get().resetPwd(phone,ticket,pwd).onResult({

            if(!isHidden){

                sendClickEvent(StatisticsId.u_dl_wjmm_czmm_done)

                btn_submit.success("设置成功"){
                    unlockLoad()

                    loginHolderViewModel.clickEvent(LoginHolderViewModel.CLICK_LOGIN)
                    ToastUtils.showText("请重新登录")
                }
            }

        },{

            if(!isHidden){

                sendClickEvent(StatisticsId.u_dl_wjmm_czmm_fail)

                btn_submit.fail("设置失败"){
                    unlockLoad()
                }

                ApiCallback.toastErrorMsg(it)
            }

        }))
    }

    private fun doRegSubmit(pwd:String){
        val phone =  loginHolderViewModel.stepState.phone
        val ticket = loginHolderViewModel.stepState.ticket

        lockLoad()

        btn_submit.loading("设置中...")

        loginHolderViewModel.addRun(Api.get().regLogin(phone,ticket,pwd).onResult({

            if(!isHidden){


                if (it != null){

                    sendClickEvent(StatisticsId.u_zc_done)

                    btn_submit.success("设置成功"){
                        unlockLoad()

                        btn_submit.postDelayed({
                            loginHolderViewModel.setUserLogined(it)
                        },500)

                    }
                }else{

                    sendClickEvent(StatisticsId.u_zc_fail)

                    btn_submit.fail("设置失败"){
                        ToastUtils.showText("设置失败，请退出重试")
                        unlockLoad()
                    }
                }

            }

        },{

            if(!isHidden){
                sendClickEvent(StatisticsId.u_zc_fail)

                btn_submit.fail("设置失败"){
                    unlockLoad()
                }

                ApiCallback.toastErrorMsg(it)
            }

        }))
    }


    override fun lockLoad() {
        super.lockLoad()
//        et_pwd.isEnabled = false
//        btn_submit.isClickable = false
    }

    override fun unlockLoad() {
        super.unlockLoad()
//        et_pwd.isEnabled = true
//        btn_submit.isClickable = true
    }

    private fun inputCheck(){

        val pwd = et_pwd.text

        if (TextUtils.isEmpty(pwd)){
            img_et_line2_action.visibility = View.GONE
        }else{
            img_et_line2_action.visibility = View.VISIBLE
        }

        btn_submit.normal(getString(R.string.next_submit))

        btn_submit.isEnabled = !TextUtils.isEmpty(pwd) && pwd?.length?:0 >=6

    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)

        if (hidden){

            KeyboardUtils.hideSoftInput(et_pwd)

        }else{

            et_pwd.text = null
            btn_submit.normal(getString(R.string.next_submit))

            unlockLoad()

            KeyboardUtils.showSoftInput(et_pwd)
        }

    }

}